#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct a
{
	char name[32];
	int age;
}peo_datas;

typedef struct datas
{
	peo_datas data;
	struct datas *next;
}link_node;

int headinsert(link_node *H);
int view(link_node *H);
int returnlink(link_node *H);
link_node *returnlink1(link_node *H);
int returnheadinsert(link_node *H,link_node *p);
int freelink(link_node *H);
int returnlink2(link_node *H);

int main(int argc, const char *argv[])
{
	link_node *H=(link_node *)malloc(sizeof(link_node));
	strcpy(H->data.name,"Hello");
	H->data.age=0;
	H->next=NULL;
	int getchoose=0;
	link_node *p=H;
	while(1)
	{
		printf("1.insert 2.view 3.return 4.return1 5.return2 \n");
		scanf("%d",&getchoose);
		switch(getchoose)
		{
			case 1:
				headinsert(H);
				break;
			case 2:
				view(H);
				break;
			case 3:
				returnlink(H);
				break;
			case 4:
				p=returnlink1(H);
				H=p;
				break;
			case 5:
				returnlink2(H);
				break;
			default:
				break;
		}
	}
	return 0;
}

int headinsert(link_node *H)
{
	link_node *p=(link_node *)malloc(sizeof(link_node));
	char inputname[32];
	int inputage;
	printf("plz input name age>>>\n");
	scanf("%s %d",inputname,&inputage);
	strcpy(p->data.name,inputname);
	p->data.age=inputage;
	
	p->next=H->next;
	H->next=p;

	return 0;
}

int view(link_node *H)
{
	link_node *p=H;
	while(p->next!=NULL)
	{
		p=p->next;
		printf("*****name = %s\n",p->data.name);
		printf("*****age  = %d\n",p->data.age);
	}

	return 0;
}
int returnlink(link_node *H)
{
	
	link_node *B=H->next;
	link_node *C=B->next;
	peo_datas flag=B->data;
	
	B->data=C->data;
	C->data=flag;

	return 0;
}

link_node *returnlink1(link_node *H)
{
	link_node *p=(link_node *)malloc(sizeof(link_node));
	strcpy(p->data.name,"Hello");
	p->data.age=0;
	p->next=NULL;
	link_node *D=H;
	while(D->next!=NULL)
	{
		D=D->next;
		returnheadinsert(D,p);
	}
	freelink(H);

	return p;
}

int returnheadinsert(link_node *H,link_node *p)
{
	link_node *q=(link_node *)malloc(sizeof(link_node));
	strcpy(q->data.name,H->data.name);
	q->data.age=H->data.age;
	
	q->next=p->next;
	p->next=q;

	return 0;
}

int freelink(link_node *H)
{
	link_node *p=H;
	while(p->next!=NULL)
	{
		p=H->next;
		H->next=p->next;
		free(p);
	}

	return 0;
}
int returnlink2(link_node *H)
{
	link_node *D=H;
	link_node *p=D->next;
	link_node *q=D->next->next;

	while(q!=NULL)
	{
		p->next=q->next;
		q->next=D->next;
		D->next=q;
		q=p->next;
	}

	return 0 ;
}


